import BaseController from './base';

export default class CategoryController extends BaseController {
  public async create() {
    const { ctx, service } = this;
    try {
      const { name } = ctx.request.body;
      if (!name) return this.fail(400, '请输入分类名称');
      const data = await service.category.create(name);
      if (data.error) return this.fail(data.status, data.error);
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }

  public async find() {
    const { ctx, service } = this;
    try {
      const data = await service.category.find();
      this.success(data);
    } catch (error) {
      ctx.throw(JSON.stringify(error), 400);
    }
  }
}
